<template>
  <div class="warehouse">
    <el-row class="head fff">
      <el-form
        :inline="true"
        :model="searchForm"
        label-position="top"
        class="search"
      >
        <el-col :span="6">
          <el-form-item label="仓库编码">
            <el-input
              v-model="searchForm.like_code"
              placeholder="请输入"
            /> </el-form-item></el-col>
        <el-col :span="6">
          <el-form-item label="仓库名称">
            <el-input
              v-model="searchForm.like_name"
              placeholder="请输入"
            /> </el-form-item></el-col>
        <el-col :span="6">
          <el-form-item label="仓库状态">
            <el-select v-model="searchForm.status" placeholder="请选择">
              <el-option
                v-for="(item, index) in warehouseStatus"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select> </el-form-item></el-col>
        <el-col :span="6" class="submit">
          <el-form-item>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item></el-col>
      </el-form>
    </el-row>
    <el-row class="table fff">
      <el-row class="add">
        <el-button
          type="success"
          @click="$router.push(`/manage-base-info/warehouse/details/null`)"
        >新增仓库</el-button>
      </el-row>
      <div v-if="isNowData" class="now-data">
        <span class="imgIcon" /><span style="color: #b5abab;">暂无数据</span>
      </div>
      <template v-else>
        <el-table
          :data="tableData.records"
          stripe
          border
          :row-style="{}"
          :header-cell-style="{
            background: '#f9f6ee',
            borderColor: '#f5efee!important',
            padding: '10px 0',
          }"
          :cell-style="{ borderColor: '#f5efee!important', padding: '10px 0' }"
        >
          <el-table-column label="序号" width="68" align="center">
            <template v-slot="{ $index }">{{ $index + 1 }}</template>
          </el-table-column>
          <el-table-column
            prop="code"
            label="仓库编码"
            align="center"
            width="150"
          />
          <el-table-column
            prop="name"
            label="仓库名称"
            align="center"
            width="150"
          />
          <el-table-column
            label="仓库类型"
            align="center"
            width="150"
          ><template v-slot="{row}">{{ getWarehouse(row.type) }}</template></el-table-column>
          <el-table-column
            prop="location"
            label="省/市/区"
            align="center"
            width="200"
          />
          <el-table-column
            prop="address"
            label="详细地址"
            align="center"
            width="150"
          />
          <el-table-column label="仓库状态" align="center" width="150">
            <template v-slot="{ row }">{{
              +row.status ? "启用" : "禁用"
            }}</template>
          </el-table-column>
          <el-table-column
            prop="surface"
            label="仓库面积m²"
            align="center"
            width="150"
          />
          <el-table-column
            prop="includedNum"
            label="库区数量"
            align="center"
            width="150"
          />
          <el-table-column
            prop="personName"
            label="负责人"
            align="center"
            width="150"
          />
          <el-table-column
            prop="phone"
            label="联系电话"
            align="center"
            width="150"
          />
          <el-table-column
            prop="updateTime"
            label="更新时间"
            align="center"
            width="150"
          />
          <el-table-column
            fixed="right"
            label="操作"
            align="center"
            width="180"
          >
            <template v-slot="{ row }">
              <span
                class="operate"
                @click="
                  $router.push(`/manage-base-info/warehouse/details/${row.id}`)
                "
              >编辑</span>
              <span class="operate center" @click="stop(row)">{{
                +row.status ? "停用" : "启用"
              }}</span>
              <span class="operate" @click="del(row)">删除</span>
            </template>
          </el-table-column>
        </el-table>
        <PageTools
          :data="tableData"
          @handleSizeChange="handleSizeChangeFn"
          @handleCurrentChange="handleCurrentChangeFn"
        />
      </template>
    </el-row>
  </div>
</template>

<script>
import { getWarehouseList, setWarehouse, delWarehouse } from '@/api/wareHouse'
import { warehouseStatus, getWarehouse } from '@/constant/warehouse'
export default {
  data() {
    return {
      warehouseStatus,
      getWarehouse,
      isNowData: true,
      searchForm: {
        current: 1,
        size: 10,
        like_code: null,
        like_name: null,
        status: null
      },
      tableData: {}
    }
  },
  created() {
    this.getWarehouseList()
  },
  methods: {
    async getWarehouseList() {
      const { data } = await getWarehouseList(this.searchForm)
      this.isNowData = !data.records.length
      this.tableData = data
    },
    stop(data) {
      this.$confirm(
        `确定要${data.status ? '停用' : '启用'}: ${data.name} 吗？`,
        `确认${data.status ? '停用' : '启用'}`
      ).then(async(_) => {
        const status = data.status ? 0 : 1
        await setWarehouse({ id: data.id, status })
        this.getWarehouseList()
        this.$message.success('状态改变成功')
      })
    },
    async del(data) {
      this.$confirm(`确定要删除: ${data.name} 吗？`, '确认删除').then(
        async(_) => {
          await delWarehouse({ 'ids[]': data.id })
          this.getWarehouseList()
          this.$message.success('删除成功')
        }
      )
    },
    onSubmit() {
      this.searchForm.current = 1
      this.getWarehouseList()
    },
    reset() {
      this.searchForm.like_code = null
      this.searchForm.like_name = null
      this.searchForm.status = null
      this.onSubmit()
    },
    handleSizeChangeFn(val) {
      this.searchForm.size = val
      this.searchForm.current = 1
      this.getWarehouseList()
    },
    handleCurrentChangeFn(val) {
      this.searchForm.current = val
      this.getWarehouseList()
    }
  }
}
</script>

<style lang="scss" scoped>
.warehouse {
  .table {
    padding: 0;
    margin-top: 20px;
    .add {
      padding: 20px 30px;
      border-bottom: 1px solid #f5efee;
    }
    .operate {
      color: #ffb200;
      cursor: pointer;
      &:hover {
        color: #ff8e00;
      }
      &.center {
        margin: 0 20px;
      }
    }
  }
}
</style>
